package com.leaf.spring.y201907.day19.zk;

import java.util.List;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

public class ZookeeperOperator {

	private ZooKeeper zooKeeper;

	public ZookeeperOperator(ZooKeeper zooKeeper) {
		this.zooKeeper = zooKeeper;
	}

	public List<String> getChildren(String path) {

		try {

			return zooKeeper.getChildren(path, true);
		} catch (KeeperException | InterruptedException e) {
			System.out.println("获取子节点失败" + e);
		}
		return null;

	}

	public void iteratorChild(final String path) {

		List<String> children = getChildren(path);
		if (children != null && !children.isEmpty()) {

			System.out.println("获取" + path + "子节点" + children);
			
			children.forEach(child -> {
				if (path.endsWith("/")) {
					iteratorChild(path + child);
				} else {
					iteratorChild(path + "/" + child);
				}
			});

		}

	}

}
